package com.vitabuybuy.tv.ui.mine.foot

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.net.loadingtype.LoadingType
import com.library.common.widget.RecycleViewDivider
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityFootPrintBinding
import com.vitabuybuy.tv.ui.mine.adapter.FootDateAdapter
import com.vitabuybuy.tv.ui.mine.foot.viewmodel.FootprintViewModel
import kotlinx.android.synthetic.main.activity_foot_print.*
import kotlinx.android.synthetic.main.activity_foot_print.refresh

class FootprintActivity : BaseActivity<FootprintViewModel, ActivityFootPrintBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_foot_print

    private var pageNumber = 1
    private val footDateAdapter by lazy(LazyThreadSafetyMode.NONE) { FootDateAdapter() }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.my_footprint))
        mBinding.viewModel = viewModel
        listview.run {
            adapter = footDateAdapter
            layoutManager = LinearLayoutManager(mContext)
            addItemDecoration(RecycleViewDivider(mContext, 1))
        }
    }

    override fun observeData() {
        viewModel.getFootPrintList(pageNumber)
        viewModel.footlist.observe(this, Observer {
            if (pageNumber == 1) {
                footDateAdapter.setNewData(it)
            } else {
                footDateAdapter.addData(it)
            }
            if (footDateAdapter.mData.isEmpty()) {
                viewModel.statusView.set(LoadingType.SHOW_EMPTY)
            }else{
                viewModel.statusView.set(LoadingType.SHOW_CONTENT)
            }
            var itemSize = 0
            for (bean in it) {
                for (item in bean.footprintList) {
                    itemSize++
                }
            }
            refresh.onLoad(itemSize)
        })
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                viewModel.getFootPrintList(pageNumber)
            }
            1 -> {
                footDateAdapter.mData[msg.arg1].footprintList.removeAt(msg.arg2)
                footDateAdapter.notifyItemChanged(msg.arg1)
            }
        }
    }

    override fun eventHandling() {
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                viewModel.getFootPrintList(pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getFootPrintList(pageNumber)
            }
        })
        footDateAdapter.onClickDelete = { parentPos, childPos ->
            viewModel.deleteFootPrint(footDateAdapter.mData[parentPos].footprintList[childPos].id,
            parentPos,childPos)
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, FootprintActivity::class.java)
        }
    }
}
